package com.cs.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cs.mapper.CourseMapper;
import com.cs.mapper.CsMapper;
import com.cs.pojo.Course;
import com.cs.pojo.Cs;
import com.cs.pojo.CsExample;
import com.cs.service.CSService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;


@Service
public class CSServiceImpl implements CSService {

    @Autowired
    private CsMapper csMapper;

    @Autowired
    private CourseMapper courseMapper;

    /***
     * @description 根据学生id查询所有选课
     *
     * @param uid  学生id

     * @date 2019/6/20 22:14
     * @return java.util.List<com.cs.pojo.Cs>
     */
    @Override
    public List<Course> getCSs(Integer uid) {
        List<Course> courses = new ArrayList<>();
        //获得uid的所有选课信息
        CsExample csExample = new CsExample();
        csExample.createCriteria().andUidEqualTo(uid);
        List<Cs> css = csMapper.selectByExample(csExample);
        for(Cs cs : css){
            Course course = courseMapper.selectByPrimaryKey(cs.getCid());
            if(course != null){
                courses.add(course);
            }
        }
        return courses;
    }

    /***
     * @description 根据学生id查询所有未选的课
     *
     * @param uid
     * @date 2019/6/21 20:33
     * @return java.util.List<com.cs.pojo.Course>
     */
    @Override
    public List<Course> getNoCss(Integer uid) {
        List<Course> courses = new ArrayList<>();
        //先查询所有的选课
        List<Course> courses1 = courseMapper.selectByExample(null);
        //查询学生已选课程
        List<Course> courses2 = getCSs(uid);
        for (Course course : courses1){
            boolean isChoose = false;
            for( Course csCourse :courses2){
                if(course.getId()==csCourse.getId()){
                    isChoose = true;
                    break;
                }
            }
            if (!isChoose){
                courses.add(course);
            }
        }
        return courses;
    }

    /***u
     * @description 选课
     *
     * @param uid  用户id
     * @param cid 课程id
     * @date 2019/6/20 22:10
     */
    @Override
    public void addCSs(Integer cid, Integer uid) {
        Cs cs = new Cs();
        cs.setCid(cid);
        cs.setUid(uid);
        //在选课之前  先增加课程的人数
        Course course = courseMapper.selectByPrimaryKey(cid);
        course.setSelected(course.getSelected()+1);
        courseMapper.updateByPrimaryKeySelective(course);
//        int i = 1/0;
        csMapper.insert(cs);
    }

    /***
     * @description 退课
     *
     * @param cid 课程id
     * @param uid 用户id
     * @date 2019/6/20 22:23
     */
    @Override
    public void quitCSs(Integer cid, Integer uid) {
        CsExample csExample = new CsExample();
        csExample.createCriteria().andUidEqualTo(uid).andCidEqualTo(cid);
        //在选课之前  先减少课程的人数
        Course course = courseMapper.selectByPrimaryKey(cid);
        course.setSelected(course.getSelected()-1);
        courseMapper.updateByPrimaryKeySelective(course);
        csMapper.deleteByExample(csExample);
    }
}
